package com.etung.iot.demo.storage.constorage;

import com.etung.iot.demo.constant.CommonConstant;
import com.etung.iot.demo.dao.RealDataMapper;
import com.etung.iot.demo.modles.ConditionStorageEntity;
import com.etung.iot.demo.modles.RealData;
import com.etung.iot.demo.utils.BigDecimalUtil;
import com.etung.iot.demo.utils.RedisUtil;
import com.etung.iot.demo.utils.StorageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 后置触发
 * 当前变量的值发生改变，且有其他变量的条件存储规则触发存储
 */
@Component
public class TriggerConditionStoreRule {
    @Autowired
    private ConditionStorageUtil conditionStorageUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private RealDataMapper realDataMapper;
    @Autowired
    private StorageUtil storageUtil;


    private boolean isNeedSaveByTriggerCondition(RealData realData) {
        boolean isNeedSave;
        String imei = realData.getImei();
        int varId = realData.getVarId();
        List<ConditionStorageEntity> conditionStorageRuleList = conditionStorageUtil.getConditionStorageByTargetVarId(imei, varId);
        if (conditionStorageRuleList != null && conditionStorageRuleList.size() > 0) {
            for (ConditionStorageEntity conditionStorageEntity : conditionStorageRuleList) {
                String cacheKey = CommonConstant.REAL_DATA_CACHE_PREFIX + imei + "_" + conditionStorageEntity.getMonitorId();
                String monitorValue = (String) redisUtil.get(cacheKey);
                if (!StringUtils.isEmpty(monitorValue)) {
                    BigDecimal currentValue = BigDecimalUtil.getBigDecimal(monitorValue);
                    if (conditionStorageEntity.getTargetType() == TargetTypeEnum.SCALE_TYPE.getValue()) {
                        isNeedSave = storageUtil.scaleTypeJudge(currentValue, conditionStorageEntity);
                    } else {
                        isNeedSave = storageUtil.varTypeJudge(imei, currentValue, conditionStorageEntity);
                    }
                    if (isNeedSave) {
                        //周期判断
                        isNeedSave = periodJudge(realData, conditionStorageEntity);
                        if (isNeedSave) return true;
                    }
                }
            }
        }
        return false;
    }


    private boolean periodJudge(RealData realData, ConditionStorageEntity conditionStorageEntity) {
        if (conditionStorageEntity.getRuleType() == ConditionRuleEnum.TRIGGER.getValue()) return true;
        //周期判断
        Long lastStoreTimestamp = conditionStorageUtil.getLastStoreTimestamp(realData.getImei(), realData.getVarId());
        Integer cycle = conditionStorageEntity.getCycle();
        if (realData.getTime().getTime() - lastStoreTimestamp >= cycle * 1000) {
            return true;
        }
        return false;
    }


    public void saveRealData(RealData realData){
        boolean isNeedSave = isNeedSaveByTriggerCondition(realData);
        if (isNeedSave){
            realDataMapper.insert(realData);
            conditionStorageUtil.saveLastStoreTimestamp(realData.getImei(),realData.getVarId(),realData.getTime().getTime());
        }
    }
}
